package jprofiler.thread;


import java.io.File;
import java.util.concurrent.ArrayBlockingQueue;
   import java.util.concurrent.BlockingQueue;
   public class ThreadPoolTest {

       /**
        *创建一个阻塞队列
        */
       final BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(10000);
       final File file;
       {
           String fileName = "D:\\Work\\ideagitxiazai\\jprofiler-demo\\src\\main\\java\\jprofiler\\thread\\DeadLockA.java";
           file = new File(fileName);
       }
       public static void main(String[] args) {
           ThreadPoolTest test3 = new ThreadPoolTest();
           Thread p = new Thread(test3.new Producer());
           p.setName("Producer");
           Thread c = new Thread(test3.new Consumer());
           c.setName("Consumer");
           p.start();
           c.start();
           try {
               Thread.sleep(30000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }

       class Producer implements Runnable {
           @Override
           public void run() {
               while (true) {
                   try {
                       //收盘子任务
                       Handler handler = new Handler(file);
                       // Get MD5
                       String md5Text = handler.getMD5();
                       for (int i = 0; i < 3; i++) {
                           //收完放入 碗柜子里
                           blockingQueue.put(1);
                       }
                       // System.out.println(Thread.currentThread().getName()
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
       }
       class Consumer implements Runnable {
           @Override
           public void run() {
               while (true) {
                   try {
                       //拿盘子洗
                       blockingQueue.take();
                       //洗的过程比较耗时
                       // Handle the MD5 Operation
                       Handler handler = new Handler(file);
                       // Get MD5
                       String md5Text = handler.getMD5();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }

           }
       }


   }